/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import hilog from '@ohos.hilog';
import Ability from '@ohos.application.Ability'
import Window from '@ohos.window'

export default class EntryAbility extends Ability {
    onCreate(want, launchParam) {
        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
        hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? '');
        hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:' + JSON.stringify(launchParam) ?? '');
    }

    onDestroy() {
        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
    }

    onWindowStageCreate(windowStage: Window.WindowStage) {
        // Main window is created, set main page for this ability
        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
        globalThis.abilityContext = this.context;
        globalThis.cacheDir = this.context.cacheDir
        globalThis.filesDir = this.context.filesDir
        globalThis.resourceManager = this.context.resourceManager
        globalThis.debug=true

        globalThis.resourceManager.getRawFileContent("RootCA.crt", (error, value) => {
            if (error != null) {
                console.log("call getRawFilesssss err ");
            } else {
                let rawFile = value;
                let data: string = ''
                for (var i = 0;i < rawFile.length; i++) {
                    let todo = rawFile[i]
                    var item = String.fromCharCode(todo);
                    data += item;
                }
                globalThis.CA = data
            }
        })


        globalThis.resourceManager.getRawFileContent("RSACA.crt", (error, value) => {
            if (error != null) {
                console.log("call getRawFilesssss err ");
            } else {
                let rawFile = value;
                let data: string = ''
                for (var i = 0;i < rawFile.length; i++) {
                    let todo = rawFile[i]
                    var item = String.fromCharCode(todo);
                    data += item;
                }
                globalThis.CA1 = data
            }
        })

//                globalThis.resourceManager.getRawFileContent("JD.pem", (error, value) => {
//                    if (error != null) {
//                        console.log("call getRawFilesssss err ");
//                    } else {
//                        let rawFile = value;
//                        let data: string = ''
//                        for (var i = 0;i < rawFile.length; i++) {
//                            let todo = rawFile[i]
//                            var item = String.fromCharCode(todo);
//                            data += item;
//                        }
//                        globalThis.CA=data
//                    }
//                })
//
//
//                globalThis.resourceManager.getRawFileContent("JDSSL.pem", (error, value) => {
//                    if (error != null) {
//                        console.log("call getRawFilesssss err ");
//                    } else {
//                        let rawFile = value;
//                        let data: string = ''
//                        for (var i = 0;i < rawFile.length; i++) {
//                            let todo = rawFile[i]
//                            var item = String.fromCharCode(todo);
//                            data += item;
//                        }
//                        globalThis.CA1=data
//                    }
//                })

        windowStage.loadContent('pages/index', (err, data) => {
            if (err.code) {
                hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.ERROR);
                hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
                return;
            }
            hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
            hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
        });
    }

    onWindowStageDestroy() {
        // Main window is destroyed, release UI related resources
        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
    }

    onForeground() {
        // Ability has brought to foreground
        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
    }

    onBackground() {
        // Ability has back to background
        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
    }
}
